//
//  MapViewController.m
//  shoesseason
//
//  Created by mac on 13-3-2.
//
//

#import "MapViewController.h"
#import "GlobalVar.h"
#import "ShopAnnotation.h"
#import "GpsPositionData.h"

#import "Common.h"

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize mid;
@synthesize type;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Gps";
    
    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
    mapView.delegate = self;
    [self.view addSubview:mapView];
    mapView.mapType = MKMapTypeStandard;
    
    [self loadData];
}

-(void)loadData
{
    [activity startAnimating];
    GpsPositionData *positionData = [[GpsPositionData alloc]init];
    positionData.ids =mid;
    positionData.type = type;
    [positionData getDataWithDataRecivedBlock:^(id data) {
        [activity stopAnimating];
        
        if ([data count]>1) {
            MKCoordinateRegion region;
            
            CLLocationDegrees maxLat = -90;
            CLLocationDegrees maxLon = -180;
            CLLocationDegrees minLat = 90;
            CLLocationDegrees minLon = 180;
            
            CLLocationCoordinate2D * locationCoodinateArr = malloc(sizeof(CLLocationCoordinate2D) * [data count]);
            int i=0;
            for (NSDictionary *dict in data) {
                ShopAnnotation *siteAnnotation = [[ShopAnnotation alloc] init];
                siteAnnotation.shopData = dict;
                CLLocationCoordinate2D currentLocation = siteAnnotation.coordinate;
                if(currentLocation.latitude > maxLat)
                    maxLat = currentLocation.latitude;
                if(currentLocation.latitude < minLat)
                    minLat = currentLocation.latitude;
                if(currentLocation.longitude > maxLon)
                    maxLon = currentLocation.longitude;
                if(currentLocation.longitude < minLon)
                    minLon = currentLocation.longitude;
                locationCoodinateArr[i++] = currentLocation;
                [mapView addAnnotation:siteAnnotation ];
            }
            
            MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:locationCoodinateArr count:[data count]];
            [mapView addOverlay:routeLine];
            
            region.center.latitude     = (maxLat + minLat) / 2;
            region.center.longitude    = (maxLon + minLon) / 2;
            region.span.latitudeDelta  = (maxLat - minLat)* 1.15;
            region.span.longitudeDelta = (maxLon - minLon)* 1.15;
            [mapView setRegion:region animated:YES];
            
        }else if ([data count]==1){
            NSDictionary *dict = [data objectAtIndex:0];
            ShopAnnotation *siteAnnotation = [[ShopAnnotation alloc] init];
            siteAnnotation.shopData = dict;
            [mapView addAnnotation:siteAnnotation ];
            MKCoordinateSpan theSpan;
            theSpan.latitudeDelta=0.01;
            theSpan.longitudeDelta=0.01;
            MKCoordinateRegion theRegion;
            theRegion.center=siteAnnotation.coordinate;
            theRegion.span=theSpan;
            [mapView setRegion:theRegion];
            [mapView selectAnnotation:siteAnnotation animated:NO];
        }else{
            [Common iToast:@"没有数据!"];
        }
    }];
}


- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[ShopAnnotation class]]) {
        static NSString* SiteAnnotationIdentifier = @"SiteAnnotationIdentifier";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:SiteAnnotationIdentifier];
        if (!pinView)
        {
            MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc]
                                                   initWithAnnotation:annotation reuseIdentifier:SiteAnnotationIdentifier] ;
            customPinView.canShowCallout = YES;
            return customPinView;
            
        }else{
            pinView.annotation = annotation;
        }
        return pinView;
    }else{
        return nil;
    }
}



@end
